package common.fengkai.filepost.aspect;

import com.alibaba.fastjson.JSONObject;
import common.fengkai.filepost.VO.R;
import common.fengkai.filepost.annotation.LogAnnotation;
import common.fengkai.filepost.bean.mysqlBean.EmpUser;
import common.fengkai.filepost.bean.mysqlBean.RequestLogEntity;
import common.fengkai.filepost.common.interfaces.Convert;
import common.fengkai.filepost.dao.mysqldao.RequestLogMapper;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.boot.logging.log4j2.SpringBootConfigurationFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author Fengkai
 * @creat 2022/2/14
 */
@Slf4j
@Aspect
@Component
public class TestAspect {

    @Pointcut("execution(* common.fengkai.filepost.controller.TestController.*(..))")
    public void pointcut(){}

    @Pointcut("@annotation(common.fengkai.filepost.annotation.PermissionsAnnotation)")
    public void premissinos(){}


    @Pointcut("@annotation(common.fengkai.filepost.annotation.AroundAnnotation)")
    public void aroundAnnotation(){};

    @Before("execution(* common.fengkai.filepost.service.Buy.buy(..))")
    public void haha(){
        log.info("买东西------------------");
    }


    @After("execution(* common.fengkai.filepost.service.Buy.buy(..))")
    public void getInfo(){
        System.out.println("ok buy same sing");
    }

    @Before("pointcut()")
    public void checkTestContrllerInfo(JoinPoint joinPoint){
        Signature signature = joinPoint.getSignature();
        log.info("切入的包名:{}", signature.getDeclaringTypeName());
        log.info("切入的方法名:{}", signature.getName());
        Object[] args = joinPoint.getArgs();
        EmpUser arg = (EmpUser) args[0];
        log.info("入参:{}", arg.getId());
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String ip = request.getRemoteAddr();
        String requestURI = request.getRequestURI();
        log.info("请求url,{}", requestURI);
        log.info("请求ip,{}", ip);
    }


    @AfterReturning(pointcut = "premissinos()", returning = "result")
    public void checkAfter(JoinPoint joinPoint, R result){
        log.info("返回值,{}", JSONObject.toJSONString(result.get("list")));
    }


    @Around("aroundAnnotation()")
    public R checkAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        Object[] args = proceedingJoinPoint.getArgs();
        EmpUser arg = (EmpUser) args[0];
        log.info("入参,{}", JSONObject.toJSONString(arg));
        arg.setId(8999);
        args[0] = arg;
        Object [] args1 = new Object[1];
        args1[0] = arg;
        R proceed = (R) proceedingJoinPoint.proceed(args1);
        log.info("返回参数,{}", JSONObject.toJSONString(proceed));
        return proceed;
    }


}
